package net.lwf.handler.wp.helper.time;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

import lwf.handler.entities.RadioData;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.time.util.Time;
import net.lwf.handler.wp.helper.IWordProblem;
import net.lwf.handler.wp.helper.WpImpl;
import net.lwf.hibernate.entity.wp.WpData;
import net.lwf.number.util.RandomNumberGenerator;

/**
 * This class aims to calculate the time difference.
 */
public class TimeOneImpl extends WpImpl implements IWordProblem {

	//lwf.handler.wp.time.type.TypeOneTimeWp|
	//pm*[NAME_1] started putting puzzle pieces together at [VALUE_1] and worked on it until [VALUE_2]. How much time had passed?
	public QuestionInfo generate(WpData wpdata) {

		QuestionInfo qinfo = new QuestionInfo();
		
		StringTokenizer stk = new StringTokenizer(wpdata.getProblem(), "*");
		String ampm = stk.nextToken();
		String question = stk.nextToken();
		
		Time time = null;
		int hour = 0;
		int multiple = RandomNumberGenerator.generate(1, IHandlerConstant.EASY_MULTIPLES.length);
		int minute = IHandlerConstant.EASY_MULTIPLES[multiple-1];
		if(ampm.equalsIgnoreCase("AM")) {
			hour = RandomNumberGenerator.generate(7, 11);
		}
		else {
			hour = RandomNumberGenerator.generate(13, 20);
		}
		time = new Time(hour, minute, 0);
		String val1 = time.getFormattedTime();
		int anshour = RandomNumberGenerator.generate(0, 2);
		int ansmultiple = RandomNumberGenerator.generate(1, IHandlerConstant.EASY_MULTIPLES.length);
		int ansminute = IHandlerConstant.EASY_MULTIPLES[ansmultiple - 1];
		time.add(anshour, ansminute, 0);
		String val2 = time.getFormattedTime();
		//Set Answer
		//qinfo.setAnswer(String.valueOf(anshour) + ":" + ansminute + " " + ampm.toUpperCase());
		qinfo.setAnswer(String.valueOf(anshour) + ":" + String.valueOf(ansminute));
		
		//Replace the place holders
		question = replacePH(question, IHandlerConstant.NAME, 1);
		String[] svalues = {val1, val2};
		question = replaceValues(question, svalues);		
		
		//Set the question
		qinfo.setQuestion(question);
		qinfo.setFormattedAnswer(this.formattedChoice(anshour, ansminute));
		qinfo.setRadioAnswerChoices(this.generateRadioAnswerChoises(anshour, ansminute));
		return qinfo;
	}
	
	private List<RadioData> generateRadioAnswerChoises(int actualhr, int actualmin) {
		String answer = String.valueOf(actualhr) + ":" + actualmin;
		List<RadioData> data = new ArrayList<RadioData>();
		data.add(new RadioData(answer, this.formattedChoice(actualhr, actualmin)));
		for(int i=0; i<3; i++) {
			int newHr = 0; int newMin = actualmin;
			if(actualhr <= 0) {
				newHr = RandomNumberGenerator.generate(0, 1);
			}
			else if(actualhr > 0 && actualhr <= 3) {
				newHr = RandomNumberGenerator.generate(0, 3);
			}
			else {
				newHr = RandomNumberGenerator.generate(0, 1);
			}
			while (newMin == actualmin) {
				newMin = RandomNumberGenerator.generate(0, 59);
			}
			
			data.add(new RadioData(String.valueOf(newHr) + ICommonConstant.COLON + newMin, this.formattedChoice(newHr, newMin)));
		}
		Collections.shuffle(data);
		return data;
	}
	
	private String formattedChoice(int hr, int min) {
		String answer = ICommonConstant.EMPTY;
		if(hr != 0) {
			if(hr > 1) {
				answer += hr + " hours ";	
			}
			else {
				answer += hr + " hour ";
			}
			
		}
		if(min != 0) {
			answer += min + " minutes";
		}
		return answer;
	}

}
